package com.itheima.d2_polymorphism;

/**
 * @ClassName Test2
 * @Author 16799
 * @Date 2025/7/10 15:39
 * @Version 1.0
 * @Description TODO
 *
 * 自己写一遍test里面的例子
 **/
public class Test2 {
    public static void main(String[] args) {
        People p2 = new Teacher();
        System.out.println("调用的是重写的run方法，就近原则。独有的方法调用不了");
        p2.run();
        //p2.teach();  报错

        System.out.println("类型转换一下,调子类自己的方法");
        Teacher t2 = (Teacher) p2;

       /* 如果乱转换子类的类型就会报错ClassCastException
       Student s = (Student) p2;*/
        //((Teacher) p2).teach();
        t2.teach();
        t2.run();

        System.out.println("==============================");

        if (p2 instanceof Teacher){
            Teacher s2 = (Teacher) p2;
            s2.teach();
        }


    }
}
